package com.skytech.tax.customsdatawarehouse.fta.origincredential.domain.service;

import com.alibaba.fastjson.JSON;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.fta.origincredential.domain.dao.FtaOriginCredentialFileRepository;
import com.skytech.tax.customsdatawarehouse.fta.origincredential.domain.entity.FtaOriginCredentialFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class FtaOriginCredentialFileService {

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private FtaOriginCredentialFileRepository ftaOriginCredentialFileRepository;

    public String originCredentialFile(String docId) {
        FtaOriginCredentialFile originCredentialFile = ftaOriginCredentialFileRepository.findByDocId(docId);
        return SecurityUtil.encrypt3DES(token, JSON.toJSONString(originCredentialFile));
    }

    public void deleteOriginCredentialFile(String docId) {
        ftaOriginCredentialFileRepository.delete(docId);
    }

    public void saveOriginCredentialFile(String docId, String file) {
        FtaOriginCredentialFile ftaOriginCredentialFile = new FtaOriginCredentialFile(docId, file);
        ftaOriginCredentialFileRepository.save(ftaOriginCredentialFile);
    }

    public boolean originCredentialFileStored(String docId) {
        return ftaOriginCredentialFileRepository.exists(docId);
    }
}
